<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function test() {
    var arr = { "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖", "0": "零", "10": "拾", };
    var a = prompt("请输入一串低于10位的数字：");
    if(a === "0"){
        console.log(arr[0]);
        return;
    }
    reg = /^[1-9][0-9]{0,8}$/;
    if (a.search(reg)) {
        console.error("请正确输入!");
        return;
    }
    var result = "";
    var flag = 0;
    var flag_dict = { "0": "", "1": "万", "2": "亿" };
    for (var i = a.length; i > 0;) {
        var son_str
        if (i - 4 > 0) {
            son_str = a.slice(i - 4, i);
            i -= 4;
        } else {
            son_str = a.slice(0, i);
            i = 0;
        }
        if(son_str != 0){
            result = exchange(son_str) + flag_dict[flag] + result;
        }
        flag++;

    }
    function exchange(son_str) {
        var pre_result = "";
        var dict = { "0": "仟", "1": "佰", "2": "拾", "3": "" };
        var i = 0;
        while (i < son_str.length) {
            if (son_str[i] != 0) {
                pre_result = pre_result + arr[son_str[i]] + dict[4 - son_str.length + i];
            } else {
                if (son_str[i + 1] != 0 && i + 1 < son_str.length)
                    pre_result = pre_result + arr[0];
            }
            i++;
        }
        return pre_result;
    }
    console.log(test_num);
    console.log(result);
}

test()
    </script>
</body>
</html>